﻿Public Class frmPayslip
    Dim payrollManager As New PayrollManager
    Dim validator As New PropertyValidator
    Dim employeeinfo As New EmployeeInfoManager
    Dim employee As EmployeeInfo
    Dim path As String = Application.StartupPath().ToString
    Private Data As Employee
    Sub New(ByVal emp As Employee)
        InitializeComponent()
        Data = emp

    End Sub
    Private Sub frmPayslip_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = frmMain
        ClearAllFields()
        Try
            Dim dataList As New List(Of Payroll)
           
            dataList = If((Data.role = "1"), payrollManager.getAllPayroll(), payrollManager.getPayrollId(Data.id))
            If Not dataList Is Nothing Then populateListView(dataList)
        Catch ex As Exception
            MsgBox("Can't load Payslip" & vbCrLf & ex.Message)
        End Try
    End Sub

    Public Function ClearAllFields()
        validator.ResetControls(grpAttendanceLogs)
        grpAttendanceLogs.Text = "Total Record(s): 0"
        validator.resizes(lvwPayroll)
        Clipboard.Clear()
        btnExport.Visible = (Data.role = "1")
        btnViewAll.Visible = (Data.role = "1")
        GroupBox2.Visible = (Data.role = "1")
        Return 0
    End Function
    Public Sub populateListView(ByVal payrollList As List(Of Payroll))
        ClearAllFields()
        Try

            For Each payroll As Payroll In payrollList
                Dim item As ListViewItem
                employee = employeeinfo.retrieveEmployeeInfoByEmployeeId(payroll.employee_id)
               
                item = lvwPayroll.Items.Add(payroll.employee_id)
                item.SubItems.Add(payroll.date_from)
                item.SubItems.Add(payroll.date_to)
                If Not employee Is Nothing Then
                    item.SubItems.Add(employee.last_name)
                    item.SubItems.Add(employee.first_name)
                    item.SubItems.Add(employee.middle_name)
                End If
                item.SubItems.Add(payroll.salary)
                item.SubItems.Add(payroll.worked_hours)
                item.SubItems.Add(payroll.basic_pay)
                item.SubItems.Add(payroll.ot_hours)
                item.SubItems.Add(payroll.ot_pay)
                item.SubItems.Add(payroll.holiday_hours)
                item.SubItems.Add(payroll.holiday_pay)
                item.SubItems.Add(payroll.nightdiff_hours)
                item.SubItems.Add(payroll.nightdiff_pay)
                item.SubItems.Add(payroll.incentive_1_name)
                item.SubItems.Add(payroll.incentive_1_amount)
                item.SubItems.Add(payroll.incentive_2_name)
                item.SubItems.Add(payroll.incentive_2_amount)
                item.SubItems.Add(payroll.incentive_3_name)
                item.SubItems.Add(payroll.incentive_3_amount)
                item.SubItems.Add(payroll.sss_amount)
                item.SubItems.Add(payroll.pagibig_amount)
                item.SubItems.Add(payroll.philhealth_amount)
                item.SubItems.Add(payroll.deduction_1_name)
                item.SubItems.Add(payroll.deduction_1_amount)
                item.SubItems.Add(payroll.deduction_2_name)
                item.SubItems.Add(payroll.deduction_2_amount)
                item.SubItems.Add(payroll.deduction_3_name)
                item.SubItems.Add(payroll.deduction_3_amount)
                item.SubItems.Add(payroll.taxable_income)
                item.SubItems.Add(payroll.net_pay)

            Next
            grpAttendanceLogs.Text = "Total Record(s): " + lvwPayroll.Items.Count.ToString()
            validator.resizes(lvwPayroll)
        Catch ex As Exception
            MsgBox("Can't load Payslip" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Sub btnExport_Click(sender As System.Object, e As System.EventArgs) Handles btnExport.Click
        Try
            Dim xls As Microsoft.Office.Interop.Excel.Application
            Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
            Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
            Dim misValue As Object = System.Reflection.Missing.Value
            Dim strDestination As String

            xls = New Microsoft.Office.Interop.Excel.Application
            'With OpenFileDialog1
            '    .Filter = "Excel Office|*.xls;*.xlsx"
            '    .FileName = ""
            '    .ShowDialog()
            '    strDestination = .FileName
            'End With

            'xlsWorkBook = xls.Workbooks.Open(path & "\payslip_template - Copy.xls")
            xlsWorkBook = xls.Workbooks.Open(path & "\payslip_template2.xls")
            xlsWorkSheet = xlsWorkBook.Sheets("sheet1")

            For i As Integer = 1 To lvwPayroll.SelectedItems.Count
                Dim payrollList As List(Of Payroll) = payrollManager.getPayrollId(lvwPayroll.SelectedItems(i - 1).SubItems(0).Text)
                For Each payroll As Payroll In payrollList
                    xlsWorkSheet.Cells(7, "C") = payroll.employee_id
                    employee = employeeinfo.retrieveEmployeeInfoByEmployeeId(payroll.employee_id)
                    xlsWorkSheet.Cells(7, "D") = employee.last_name
                    xlsWorkSheet.Cells(7, "F") = employee.first_name
                    xlsWorkSheet.Cells(7, "I") = employee.middle_name
                    xlsWorkSheet.Cells(10, "C") = payroll.date_from.ToString()
                    xlsWorkSheet.Cells(11, "C") = payroll.date_to.ToString()
                    xlsWorkSheet.Cells(14, "C") = payroll.salary
                    xlsWorkSheet.Cells(16, "C") = payroll.worked_hours
                    xlsWorkSheet.Cells(18, "C") = payroll.basic_pay
                    xlsWorkSheet.Cells(15, "F") = payroll.ot_hours
                    xlsWorkSheet.Cells(15, "G") = payroll.ot_pay
                    xlsWorkSheet.Cells(16, "F") = payroll.nightdiff_hours
                    xlsWorkSheet.Cells(16, "G") = payroll.nightdiff_pay
                    xlsWorkSheet.Cells(21, "E") = payroll.incentive_1_name
                    xlsWorkSheet.Cells(21, "G") = payroll.incentive_1_amount
                    xlsWorkSheet.Cells(22, "E") = payroll.incentive_2_name
                    xlsWorkSheet.Cells(22, "G") = payroll.incentive_2_amount
                    xlsWorkSheet.Cells(23, "E") = payroll.incentive_3_name
                    xlsWorkSheet.Cells(23, "G") = payroll.incentive_3_amount
                    xlsWorkSheet.Cells(14, "J") = payroll.sss_amount
                    xlsWorkSheet.Cells(15, "J") = payroll.pagibig_amount
                    xlsWorkSheet.Cells(16, "J") = payroll.philhealth_amount
                    xlsWorkSheet.Cells(21, "H") = payroll.deduction_1_name
                    xlsWorkSheet.Cells(21, "J") = payroll.deduction_1_amount
                    xlsWorkSheet.Cells(22, "H") = payroll.deduction_2_name
                    xlsWorkSheet.Cells(22, "J") = payroll.deduction_2_amount
                    xlsWorkSheet.Cells(23, "H") = payroll.deduction_3_name
                    xlsWorkSheet.Cells(23, "J") = payroll.deduction_3_amount
                    xlsWorkSheet.Cells(25, "J") = payroll.taxable_income
                    xlsWorkSheet.Cells(27, "J") = payroll.net_pay
                Next
            Next
            With SaveFileDialog1
                .Filter = "Excel Office|*.xls;*.xlsx"
                .FileName = ""
                .ShowDialog()
                strDestination = .FileName
            End With
            xlsWorkBook.SaveAs(strDestination)
            xlsWorkBook.Close()
            xls.Quit()
        Catch ex As Exception
            MsgBox("Can't lod Web page" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Sub btnExecuteSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnExecuteSearch.Click
        Dim fromDate As String = dtpFrom.Value.ToString("yyyy-M-d HH:mm:ss")
        Dim toDate As String = dtpTo.Value.ToString("yyyy-M-d HH:mm:ss")

        Dim dataList As List(Of Payroll) = payrollManager.getPayrollByDate(Data.id, fromDate, toDate)
        populateListView(dataList)
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click



        Dim byEmpName As String = "" + txtByEmpName.Text
        Dim byEmpNo As String = "" + txtByEmpNo.Text

        If byEmpName.Length <= 0 And byEmpNo.Length <= 0 Then
            Return
        End If


        Dim employeeIdArray As New List(Of String)

        If byEmpName.Length > 0 Then
            Dim empInfo As List(Of EmployeeInfo) = employeeinfo.retrieveEmployeeInfoArrayByName(byEmpName)
            For Each info As EmployeeInfo In empInfo
                If Not employeeIdArray.Contains(info.employee_id.ToString) Then
                    employeeIdArray.Add(info.employee_id.ToString)
                End If
            Next
        End If

        If byEmpNo.Length > 0 Then
            Dim empInfo As List(Of EmployeeInfo) = employeeinfo.retrieveEmployeeInfoArrayByEmployeeNo(byEmpNo)
            For Each info As EmployeeInfo In empInfo
                If Not employeeIdArray.Contains(info.employee_id.ToString) Then
                    employeeIdArray.Add(info.employee_id.ToString)
                End If
            Next
        End If


        Dim dataList As List(Of Payroll) = payrollManager.retrievePayrollByEmployeeIDArray(employeeIdArray)

        populateListView(dataList)

    End Sub

    Private Sub btnViewAll_Click(sender As System.Object, e As System.EventArgs) Handles btnViewAll.Click
        frmPayslip_Load(vbNull, EventArgs.Empty)
    End Sub

   
End Class